﻿﻿@{
    ViewData["Title"] = "Shee_YardShow";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<h1 style="text-align:center">羊场列表</h1>

<div style="margin:20px 20px">
    <body>
        <table>
            <tr>
                <td>
                    <label class="layui-form-label">羊场全称</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" id="SheepFull" autocomplete="off">
                    </div>
                </td>
                <td>
                    <button type="button" class="layui-btn layui-btn-primary" id="searchBtn" style="background-color:aqua"><i class="layui-icon"></i> 搜 索</button>
                    <div class="layui-inline">
                        <button type="button" class="layui-btn" onclick="onAddBtn()"><i class="layui-icon layui-icon-file">羊场新增</i></button>
                    </div>

                </td>
            </tr>
        </table>

        <table id="ID-table-demo-setRowChecked"></table>
        <div id="pagesbox"></div>

        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

        <div id="add-main" style="display: none;">

            <form class="layui-form" id="add-form" action="" lay-filter="dataPage">

                <div class="layui-form-item">

                    <div class="layui-form-item">
                        <div class="layui-form-item">
                            <div class="layui-input-group">
                                <div class="layui-input-prefix">
                                    羊场全称
                                </div>
                                <input type="text" id="sheepFull" name="sheepFull" placeholder="请选择羊场全称" lay-verify="required" autocomplete="off" class="layui-input" style="width:500px">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-form-item">
                            <div class="layui-input-group">
                                <div class="layui-input-prefix">
                                    羊场简称
                                </div>
                                <input type="text" name="sheepAbbre" id="sheepAbbre" placeholder="请选择羊场简称" lay-verify="required" autocomplete="off" class="layui-input" style="width:500px">
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-form-item">
                            <div class="layui-input-group">
                                <div class="layui-input-prefix">
                                    详细地址
                                </div>
                                <input type="text" name="sheepAddress" id="sheepAddress" placeholder="请选择详细地址" autocomplete="off" lay-verify="required" class="layui-input" style="width:500px">
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-form-item">
                            <div class="layui-input-group">
                                <div class="layui-input-prefix">
                                    建账日期：
                                </div>
                                <div class="layui-input-block" >
                                    <div class="layui-input-inline">
                                        <input type="text" class="layui-input" id="sheeAccounts" style="width:480px" autocomplete="off" name="sheeAccounts" placeholder="yyyy-MM-dd">
                                    </div>
                                </div>
                            </div>
                        </div>


                    <div class="layui-form-item">
                        <div class="layui-form-item">
                            <div class="layui-input-group">
                                <div class="layui-input-prefix">
                                    管理部门
                                </div>
                                <div class="layui-input-block" style="width:500px">
                                    <select name="departId" id="rommss" lay-filter="breedSelect">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-form-item">
                                <div class="layui-input-group">
                                    <div class="layui-input-prefix">
                                        状态
                                    </div>

                                    <div class="layui-input-block">
                                        <input type="radio" id="sheepState" name="sheepState" value="1" title="启用" checked>
                                        <input type="radio" id="sheepState" name="sheepState" value="0" title="禁用">
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <input type="hidden" name="sheepCode" />
                        <input type="hidden" name="sheepId" />

                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="save">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary" id="closeBtn">关闭</button>
                            </div>
                        </div>
                    </div>
            </form>

            <script>
                layui.use(['table', 'laypage', 'layer', 'form', 'layer','laydate'], function () {
                    var table = layui.table;
                    var laypage = layui.laypage;
                    var obj = {};
                    var form = layui.form;
                    var layer = layui.layer; // 添加layui弹出层模块
                    var laydate = layui.laydate;
                    // 渲染
                    laydate.render({
                        elem: '#sheeAccounts'
                    });

                    var size = 15; // 每页显示条数
                    //获取列表
                    function getListData(page) {
                        //获取查询条件
                        var SheepFull = $('#SheepFull').val();
                        var SheepScale = $('#SheepScale').val();
                        //ajax后台交互
                        $.ajax({
                            url: 'http://10.31.60.18:8000/AuthorizationGetRout/api/Shee_Yand/GetShowShee_Yard',
                            type: 'get',
                            dataType: 'json',
                            //参数
                            data: {
                                page: page,
                                size: size,
                                SheepFull: SheepFull,

                            },
                            success: function (res) {
                                table.render({
                                    //挂载的表格id
                                    elem: '#ID-table-demo-setRowChecked',
                                    cols: [[
                                        { type: 'checkbox', width: 60, fixed: 'center', unresize: true, align: 'center' },
                                        { field: 'number', type: 'numbers', title: '序号', width: 60, fixed: 'center', unresize: true, align: 'center', templet: '#zizeng' },
                                        { field: 'sheepCode', title: '羊场编码', width: 150, align: 'center' },
                                        { field: 'sheepFull', title: '羊场全称', width: 120, align: 'center' },
                                        { field: 'sheepAbbre', title: '羊场简称', width: 120, align: 'center' },
                                        { field: 'sheeAccounts', title: '建账日期', width: 120, align: 'center' },
                                        {

                                            field: 'uid', title: '负责人', width: 120, align: 'center',
                                            templet: function (data) {
                                                if (data.uid == 1) return '张三';
                                                if (data.uid == 2) return '李四';
                                                if (data.uid == 3) return '王五';
                                            }

                                        },
                                        { field: 'sheepAddress', title: '详细地址', width: 120, align: 'center' },
                                        {
                                            field: 'departId', title: '管理部门', width: 120, align: 'center',
                                            templet: function (data) {
                                                if (data.departId == 1) return '测试引入';
                                                if (data.departId == 2) return '管理部门';
                                                if (data.departId == 3) return '技术总监';
                                            }

                                        },
                                        { field: 'createBy', title: '创建人', width: 120, align: 'center' },
                                        { field: 'createDate', title: '创建时间', width: 120, align: 'center' },
                                        { field: 'updateBy', title: '修改人', width: 120, align: 'center' },
                                        { field: 'updateDate', title: '修改时间', width: 120, align: 'center' },
                                        { title: '操作', width: 200, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true },

                                    ]],
                                    toolbar: '#demo-toolbar-setRowChecked',
                                    initSort: { // 设置初始排序
                                        field: 'experience', // 字段名
                                        type: 'asc' // 倒序
                                    },
                                    data: res.data
                                });
                                //分页
                                laypage.render({
                                    //挂载的分页id
                                    elem: 'pagesbox',
                                    count: res.tatalCount,
                                    limit: size,
                                    curr: page,
                                    layout: ['count', 'prev', 'page', 'next', 'skip'],
                                    jump: function (obj, first) {
                                        if (!first) {
                                            getListData(obj.curr);
                                        }
                                    }
                                });

                            }
                        });
                    }

                    $('#searchBtn').on('click', function () {
                        getListData(1);
                    });

                    //管理部门
                    $(document).ready(function () {
                        $.ajax({
                            url: 'http://10.31.60.18:8000/AuthorizationGetRout/api/Shee_Yand/AdministrativeType', // 后端枚举数据接口地址
                            type: 'get',
                            dataType: 'json',
                            success: function (res) {
                                var enumData = res; // 直接使用返回的数据
                                var select = $('#rommss');
                                $.each(enumData, function (index, item) {
                                    // 使用正确的字段添加选项到下拉框中
                                    var option = $('<option>').val(item.value).text(item.text);
                                    select.append(option);
                                });
                                // 手动触发 layui 组件的渲染
                                layui.form.render('select');
                            },
                            error: function () {
                                // 处理 AJAX 请求失败的情况
                                console.error('Failed to fetch data for dropdown.');
                            }
                        });
                    });

                    // 单删除
                    function deleteData(sheepId) {
                        $.ajax({
                            url: `http://10.31.60.18:8000/AuthorizationSetRout/api/Shee_Yand/Shee_YandDel?sheepId=${sheepId}`,
                            type: 'delete',
                            success: function (res) {
                                if (res > 0) {
                                    layer.msg('删除成功');
                                    getListData(1);
                                } else {
                                    layer.msg('删除失败');
                                    getListData(1);
                                }
                            },
                            error: function () {
                                layer.msg('删除请求失败');
                            }
                        });
                    }

                    // 监听工具条，实现删除功能及修改
                    table.on('tool(ID-table-demo-setRowChecked)', function (obj) {
                        var data = obj.data;
                        var sheepId = data.sheepId; // 获取当前行数据的唯一标识符

                        if (obj.event == 'edit') {
                            editData(data.sheepId); // 传递正确的数据项ID进行编辑操作
                        }
                        if (obj.event == 'delete') {
                            layer.confirm('确定删除吗？', function (index) {
                                deleteData(data.sheepId); // 传递的数据项ID进行删除操作
                            });
                        }
                    });

                    // 初始化加载数据
                    $(function () {
                        getListData(1);
                    })
                    //配置插件目录
                    layui.config({
                        base: '/mods/'
                    });
                    layui.use(['form'], function () {
                        var obj = {};
                        var form = layui.form;
                        // 提交事件
                        form.on('submit(save)', function (data) {
                            if ($("[name='sheepId']").val() == "") {
                                var field = data.field;
                                obj.sheepFull = field.sheepFull;
                                obj.sheepAbbre = field.sheepAbbre; 
                                obj.departId = field.departId; 
                                obj.sheeAccounts = field.sheeAccounts;
                                obj.sheepAddress = field.sheepAddress;
                                obj.sheepState = field.sheepState;
                                console.log(field);
                                $.post({
                                    url: "http://10.31.60.18:8000/AuthorizationSetRout/api/Shee_Yand/Shee_YandCreate",
                                    data: JSON.stringify(obj),
                                    contentType: "application/json",
                                    success: res => {
                                        if (res > 0) {
                                            var icon = 0;
                                            layer.alert('添加成功', {
                                                icon: 1,
                                                shadeClose: true,
                                                title: "羊场新增",
                                            }, function () {
                                                location.href = "/Shee_Yard/Shee_YardShow";
                                            });

                                        } else {
                                            alert("添加失败");
                                        }
                                    }
                                })
                            }
                            else {
                                updateSheepYard();
                            }
                            return false; // 阻止默认 form 跳转
                        });
                    });
                    // 重置按钮点击事件
                    $('#closeBtn').on('click', function () {
                        location.href = "/Shee_Yard/Shee_YardShow";
                    });
                });
                //羊场新增
                function onAddBtn() {
                    $('#add-form')[0].reset();
                    var laydate = layui.laydate;
                    // 渲染
                    laydate.render({
                        elem: '#storageDate'
                    });
                    layer.open({
                        type: 1,
                        title: "羊场设置",
                        shift: 2,
                        area: ['600px', '450px'],
                        shade: 0,
                        shadeClose: false,
                        content: $("#add-main"),
                        success: function (layero, index) {

                        }
                    });
                }


                //数据回显
                function editData(sheepId) {
                    $.ajax({
                        url: 'http://10.31.60.18:8000/AuthorizationGetRout/api/Shee_Yand/FindShee_Yard?SheepId=' + sheepId,
                        type: 'get',
                        contentType: 'application/json',
                        success: function (res) {
                            if (res != null) {
                                layer.open({
                                    type: 1,
                                    title: "修改新记录",
                                    shift: 2,
                                    closeBtn: false,
                                    area: ['600px', '450px'],
                                    shade: 0,
                                    shadeClose: false,
                                    content: $("#add-main"),
                                    success: function (layero, index) {

                                    }
                                });
                                $("[name='sheepId']").val(res.sheepId);
                                $("#sheepCode").val(res.sheepCode);
                                $("#sheepFull").val(res.sheepFull);
                                $("#sheepAbbre").val(res.sheepAbbre); 

                                $("#sheeAccounts").val(res.sheeAccounts.split('T')[0]);

                                $("#cottageRemark").val(res.cottageRemark);
                                $("#sheepAddress").val(res.sheepAddress);
                                $("#rommss").val(res.departId);
                                layui.use(['form'], function () {
                                    var form = layui.form;
                                    $("[name='departId'][value='" + res.departId + "']").prop("checked", "checked");
                                    form.render();
                                });

                                $("#sheepState").val(res.sheepState);

                                console.log(res)

                            } else {
                                layer.msg(res.msg, { icon: 5 });
                            }
                        },
                        error: function (err) {
                            // 处理请求错误
                        }
                    });
                }

                // 监听表单修改
                function updateSheepYard() {
                    // var earNumber = data.field.earNumber;
                    var formData = {
                        sheepId: $("[name='sheepId']").val(), // 获取主键id
                        sheepCode: $("#sheepCode").val(), 
                        sheepFull: $("#sheepFull").val(),
                        sheepAbbre: $("#sheepAbbre").val(),
                        sheeAccounts: $("#sheeAccounts").val(),
                        departId: $("#rommss").val(),
                        sheepAddress: $("#sheepAddress").val(),
                        sheepState: $("#sheepState").val() == "true" ? true : false,
                    };

                    $.ajax({
                        url: 'http://10.31.60.18:8000/AuthorizationSetRout/api/Shee_Yand/UnitsUpd', // 替换为您的后端API接口地址
                        type: 'put',
                        data: JSON.stringify(formData),
                        contentType: 'application/json',
                        success: function (res) {
                            if (res == 1) {
                                layer.msg("修改成功", { icon: 1 });
                                setTimeout(function () {
                                    location.href = '/Shee_Yard/Shee_YardShow'
                                }, 1000)
                            } else {
                                layer.msg("修改失败", { icon: 2 });
                            }
                        },
                        error: function () {
                            layer.msg('网络错误，请稍后重试', { icon: 2 });
                        }
                    });
                    return false; // 阻止表单跳转
                };


            </script>


            <script type="text/html" id="barDemo">
                <div class="layui-btn-container">
                   <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit">编辑</i></a>
                   <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete"><i class="layui-icon layui-icon-delete">删除</i></a>
                </div>
            </script>

    </body>
</div>